import addDetailCard from "./add-detail-card";
import addCustomDetailCard from "./add-customdetail-card";
import addNoDetailCard from "./add-nodetail-card";
import cubeCardClear from "./cube-card-clear";
import setBackColor from "./set-back-color";
import setBold from "./set-bold";
import setBorder from "./set-border";
import setCellSpan from "./set-cell-span";
import setFontFamily from "./set-font-family";
import setFontSize from "./set-font-size";
import setFontType from "./set-font-type";
import setForeColor from "./set-fore-color";
import setHAlign from "./set-h-align";
import setItalic from "./set-italic";
import setUnderline from "./set-underline";
import setVAlign from "./set-v-align";
import setWordwrap from "./set-wordwrap";

import setFixedCell from "./set-fixedCell";
import setFormatter from "./set-formatter";

import formatpainter from './formatpainter'
import edcExcelTableInsertCol from "./edc-excel-tableInsertCol";
import edcExcelTableInsertRow from "./edc-excel-tableInsertRow";
import edcExcelTableDeleteRow from "./edc-excel-tableDeleteRow";
import edcExcelTableDeleteCol from "./edc-excel-tableDeleteCol";
import edcExcelCusTableInsertCol from "./edc-excel-custableInsertCol";
import edcExcelCusTableDeleteCol from "./edc-excel-custableDeleteCol";
import edcExcelCusTableInsertRow from "./edc-excel-custableInsertRow";
import edcExcelCusTableDeleteRow from "./edc-excel-custableDeleteRow";
import changeCellType from "./change-cell-type";
import autoSignField from "./auto-sign-field";
import deleteDetailCard from "./delete-detail-card";
import clearContent from "./clear-content";
import { WeaLocaleProvider } from "ecCom";
import excelInsertRow from "./excel-insert-row";
import excelInsertColumn from "./excel-insert-column";
import excelDeleteRow from "./excel-delete-row";
import excelDeleteColumns from "./excel-delete-columns";
import excelRowSize from "./excel-row-size";
import excelColumnSize from "./excel-column-size";
import excelHideRow from "./excel-hide-row";
import excelUnhideRow from "./excel-unhide-row";
import excelHideCol from "./excel-hide-col";
import excelUnhideCol from "./excel-unhide-col";
const getLabel = WeaLocaleProvider.getLabel;


export const getRange = (sheet, row, col) => {
    const span = sheet.getSpan(row, col);
    if (span) {
        return sheet.getRange(span.row, span.col, span.rowCount, span.colCount);
    } else {
        return sheet.getCell(row, col);
    }
}

export const register = (commandManager) => {
    commandManager.register('setFontFamily', setFontFamily);
    commandManager.register('setFontSize', setFontSize);
    commandManager.register('setBold', setBold);
    commandManager.register('setItalic', setItalic);
    commandManager.register('setUnderline', setUnderline);
    commandManager.register('setHAlign', setHAlign);
    commandManager.register('setVAlign', setVAlign);
    commandManager.register('setBorder', setBorder);
    commandManager.register('setBackColor', setBackColor);
    commandManager.register('setForeColor', setForeColor);
    commandManager.register('setCellSpan', setCellSpan);
    commandManager.register('addDetailCard', addDetailCard);
    commandManager.register('addCustomDetailCard', addCustomDetailCard);
    commandManager.register('addNoDetailCard', addNoDetailCard)
    commandManager.register('setFontType', setFontType);
    commandManager.register('cubeCardClear', cubeCardClear);
    commandManager.register('setWordwrap', setWordwrap);
    commandManager.register('setFixedCell', setFixedCell);
    commandManager.register('setFormatter', setFormatter);
    commandManager.register('edc.excel.tableInsertCol', edcExcelTableInsertCol);
    commandManager.register('edc.excel.tableInsertRow', edcExcelTableInsertRow);
    commandManager.register('edc.excel.tableDeleteRow', edcExcelTableDeleteRow);
    commandManager.register('edc.excel.tableDeleteCol', edcExcelTableDeleteCol);
    commandManager.register('edc.excel.custableInsertCol', edcExcelCusTableInsertCol);
    commandManager.register('edc.excel.custableInsertRow', edcExcelCusTableInsertRow);
    commandManager.register('edc.excel.custableDeleteRow', edcExcelCusTableDeleteRow);
    commandManager.register('edc.excel.custableDeleteCol', edcExcelCusTableDeleteCol);
    commandManager.register('excel.insert.row', excelInsertRow);
    commandManager.register('excel.insert.column', excelInsertColumn);
    commandManager.register('excel.delete.row', excelDeleteRow);
    commandManager.register('excel.delete.column', excelDeleteColumns);
    commandManager.register('excel.row.size', excelRowSize);
    commandManager.register('excel.column.size', excelColumnSize);
    commandManager.register('excel.hide.row', excelHideRow);
    commandManager.register('excel.unhide.row', excelUnhideRow);
    commandManager.register('excel.hide.col', excelHideCol);
    commandManager.register('excel.unhide.col', excelUnhideCol);
    commandManager.register('changeCellType', changeCellType);
    commandManager.register('autoSignField', autoSignField);
    commandManager.register('deleteDetailCard', deleteDetailCard);
    commandManager.register('clearContent', clearContent);
    commandManager.register('formatpainter', formatpainter);
}
export const runCommand = (workbook, options, isUndo, doFunc, undoFunc) => {
    let rt = false;
    const { sheetName } = options;
    const sheet = workbook.getSheetFromName(sheetName);
    try {
        if (!!sheet) {
            sheet.suspendPaint();
            const commandName = window.GC.Spread.Sheets.Commands.bWa(sheetName)
            if (isUndo) {
                undoFunc && undoFunc(sheet, options);
                sheet.ITa.undo(options[commandName]);
            } else {
                sheet.ITa.startTransaction();
                rt = doFunc(sheet, options);
                options[commandName] = sheet.ITa.endTransaction();
            }
            sheet.resumePaint();
        }
    } catch (e) {
        console.log(e);
    }
    return rt;
}
export const menuData = store => [{
    key: 'insert',
    title: '插入',
    icon: 'icon-edc-common-insert-row',
    number: 1,
    children: [{
        key: 'excelInsertAbove',
        title: getLabel('502985', '插入行'),
        icon: 'icon-edc-common-insertrowabove',
        number: 1,
        showNumber: true
    }, {
        key: 'excelInsertLeft',
        title: getLabel('502986', '插入列'),
        icon: 'icon-edc-common-insertrowleft',
        number: 1,
        showNumber: true
    }]
}, {
    key: 'delete',
    title: '删除',
    icon: 'icon-edc-common-deleterow',
    children: [{
        key: 'excelDeleteRow',
        title: getLabel('506303','删除行'),
        icon: 'icon-edc-common-deleterow',
    }, {
        key: 'excelDeleteColumn',
        title: getLabel('511754','删除列'),
        icon: 'icon-edc-common-deletecolumn',
    }]
}, {
    key: 'excelRowSize',
    title: getLabel('514593','设置行高'),
    icon: 'icon-edc-common-colum-height',
    showNumber: true
}, {
    key: 'excelColumnSize',
    title: getLabel('514594','设置列宽'),
    icon: 'icon-edc-common-column-width',
    showNumber: true
}] /*[{
    name: "edc.excel.insert",
    text: getLabel('502984', '插入'),
    workArea: "viewport",
    subMenu: [{
        command: "gc.spread.contextMenu.insertRows",
        text: `<div style='width:180px;'>
                     ${getLabel('502985', '插入行')}
                     <div><input type="number" /></div>
                </div>`,
        name: "edc.excel.insertLine",
        workArea: "viewport"
    }, {
        command: "gc.spread.contextMenu.insertColumns",
        text: `<div style='width:180px;'>
                    ${getLabel('502986', '插入列')}
                    <div><input type="number" /></div>
                </div>`,
        name: "edc.excel.insertRow",
        workArea: "viewport"
    }]
}, {
    name: "edc.excel.tableInsert",
    text: getLabel('502984', '插入'),
    workArea: "viewporttable",
    subMenu: [{
        command: store['edc.excel.tableInsertLeftCol'],
        text: getLabel('502987', '在左侧插入表列'),
        name: "edc.excel.tableInsertLeftCol",
        workArea: "viewporttable",
    }, {
        command: store['edc.excel.tableInsertRightCol'],
        text: getLabel('502988', '在右侧插入表列'),
        name: "edc.excel.tableInsertRightCol",
        workArea: "viewporttable",
    }, {
        command: store['edc.excel.tableInsertTopRow'],
        text: getLabel('502989', '在上方插入表行'),
        name: "edc.excel.tableInsertTopRow",
        workArea: "viewporttablenofirstdatarow",
    }, {
        command: store['edc.excel.tableInsertBottomRow'],
        text: getLabel('502990', '在下方插入表行'),
        name: "edc.excel.tableInsertBottomRow",
        workArea: "viewporttable",
    }]
}, {
    name: "edc.excel.tableDelete",
    text: getLabel(502286, '删除'),
    workArea: "viewporttable",
    subMenu: [{
        command: store['edc.excel.tableDeleteCol'],
        text: getLabel('502991', '删除表列'),
        name: "edc.excel.tableDeleteCol",
        workArea: "viewporttable",
    }, {
        command: store['edc.excel.tableDeleteRow'],
        text: getLabel('502992', '删除表行'),
        name: "edc.excel.tableDeleteRow",
        workArea: "viewporttablenofirstdatarow",
    }]
}, {
    name: "edc.excel.customtbInsert",
    text: getLabel('502984', '插入'),
    workArea: "viewportcustomtb",
    subMenu: [{
        command: store['edc.excel.tableInsertLeftCol'],
        text: getLabel('502987', '在左侧插入表列'),
        name: "edc.excel.tableInsertLeftCol",
        workArea: "viewportcustomtb",
    }, {
        command: store['edc.excel.tableInsertRightCol'],
        text: getLabel('502988', '在右侧插入表列'),
        name: "edc.excel.tableInsertRightCol",
        workArea: "viewportcustomtb",
    }, {
        command: store['edc.excel.tableInsertBottomRow'],
        text: getLabel('502990', '在下方插入表行'),
        name: "edc.excel.tableInsertBottomRow",
        workArea: "viewportcustomtb",
    }]
}, {
    name: "edc.excel.customtableDelete",
    text: getLabel(502286, '删除'),
    workArea: "viewportcustomtb",
    subMenu: [{
        command: store['edc.excel.tableDeleteCol'],
        text: getLabel('502991', '删除表列'),
        name: "edc.excel.tableDeleteCol",
        workArea: "viewportcustomtb",
    }, {
        command: store['edc.excel.tableDeleteRow'],
        text: getLabel('502992', '删除表行'),
        name: "edc.excel.tableDeleteRow",
        workArea: "viewportcustomtbnofirstdatarow",
    }]
}]*/
export const setRightMenu = (store, isTable, isFirstDataRow) => {
    const datas = menuData(store);
    if (isTable === '1') {
        if (isFirstDataRow) {
            store.menuData = datas.filter(d => {
                if (d.subMenu) {
                    d.subMenu = d.subMenu.filter(f => {
                        return /table/i.test(f.workArea) && !(/nofirstdatarow/i.test(f.workArea))
                    });
                }
                return /table/i.test(d.workArea) && !(/nofirstdatarow/i.test(d.workArea))
            });
        } else {
            store.menuData = datas.filter(d => /table/i.test(d.workArea))
        }
    } else if (isTable === '2') {
        store.menuData = datas.filter(d => /customtb/i.test(d.workArea))
    } else {
        store.menuData = datas.filter(d => d.workArea !== 'viewporttable' && d.workArea !== 'viewportcustomtb');
    }
}

export const toCells = (range) => {
    const cells = [];
    const { row, col, rowCount, colCount, sheet } = range;
    for (let rowIndex = 0; rowIndex < rowCount; rowIndex++) {
        const currentRow = row + rowIndex;
        for (let colIndex = 0; colIndex < colCount; colIndex++) {
            const currentCol = col + colIndex;
            const span = sheet.getSpan(currentRow, currentCol);
            //if (!span || (span.row === currentRow && span.col === currentCol)) {
                cells.push(sheet.getCell(currentRow, currentCol));
            //}
        }
    }
    return cells;
}